package com.monkey.core.exception;

import com.monkey.core.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * desc:
 * auth: chenxiaobin
 * data: 2017/4/1.
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<String, Object> handle(HttpServletRequest req, Exception e) throws Exception {
        logger.info("error:", e);
        Map<String, Object> ret = new HashMap<>();
        ret.put("code", ErrorCode.EXCEPTION_ERROR);
        ret.put("msg", ErrorCode.getMsg(ErrorCode.EXCEPTION_ERROR));
        return ret;
    }
}
